package com.code.modules.base.controller;

import com.code.common.utils.ToolUtil;
import com.code.common.vo.ResultData;
import com.code.config.Swagger.params.ApiJsonObject;
import com.code.config.Swagger.params.ApiJsonProperty;
import com.code.modules.base.model.Resource;
import com.code.modules.base.service.ResourceService;
import com.code.modules.base.vo.ResourceVo;
import com.code.modules.base.vo.TreeNodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@RestController
@RequestMapping("/resource")
@Api(value = "资源控制器", tags = {"资源接口"})
public class ResourceController {

    @Autowired
    private ResourceService resourceService;

    @ApiOperation("根据id查询资源")
    @RequiresAuthentication
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/findById")
    public ResultData findResourceById(@ApiJsonObject(name = "Resource_findResourceById_Map", value = {
            @ApiJsonProperty(key = "resid",  example = "1",  description = "资源ID", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer resid = (Integer)params.get("resid");
        Resource res = resourceService.selectById(resid);
        int pid = resourceService.findResIdByCode(res.getPcode());
        if(res != null) {
            ResourceVo resvo = new ResourceVo();
            BeanUtils.copyProperties(res,resvo);
            resvo.setPid(pid);
            return new ResultData().succ(200,"查询成功", resvo);
        }
        return new ResultData().fail(400,"查询失败");
    }

    @ApiOperation("通过角色id获取资源列表")
    @RequiresAuthentication
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/getresidlist")
    public ResultData getResidlist(@ApiJsonObject(name = "Resource_getResidlist_Map", value = {
            @ApiJsonProperty(key = "roleid",  example = "1",  description = "角色ID", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer roleid =  (Integer) params.get("roleid");
        List<Resource> resrolelist = resourceService.findResourceByRoleid(roleid);
        List<Integer> residlist = new ArrayList<Integer>();
        for(Resource r : resrolelist) {
            residlist.add(r.getId());
        }
        return new ResultData().succ(200,"查询成功", residlist);
    }


    @ApiOperation("资源列表树")
    @RequiresAuthentication
    @RequiresPermissions("resource:allTree")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/allTree")
    public ResultData allTree(@ApiJsonObject(name = "Resource_allTree_Map", value = {
            @ApiJsonProperty(key = "condition",  example = "查询资源名称",  description = "查询资源名称", type = String.class, required = true) })
            @RequestBody Map<String,Object> params) {
        String condition = (String)params.get("condition");
        List<TreeNodeVo> res_tree = resourceService.resTreeNodedata(condition);

        if(res_tree != null){
            return new ResultData().succ(200,"查询成功",res_tree);
        }else{
            return new ResultData().fail(400,"查询失败");
        }
    }

    @ApiOperation("添加资源")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @RequiresAuthentication
    @RequiresPermissions("resource:add")
    @PostMapping("/add")
    public ResultData addResource(@RequestBody ResourceVo resvo) {

        String checkRescode = resourceService.checkResByCode(resvo.getCode());
        if(ToolUtil.isNotEmpty(checkRescode)) {
            return new ResultData().fail(400,"资源编号已存在");
        }
        resSetPcode(resvo);
        resvo.setCreatetime(new Date());
        Resource res = new Resource();
        BeanUtils.copyProperties(resvo,res);
        resourceService.insert(res);
        return new ResultData().fail(200,"添加成功");
    }


    /**
     * 根据请求的父级菜单编号设置pcode和层级
     */
    public void resSetPcode(ResourceVo res) {
        if(ToolUtil.isEmpty(res.getPcode()) || res.getPcode().equals("0")) {
            res.setPcode("0");
            res.setPcodes("[0],");
            res.setLevels(0);
        } else {
            //通过pcodes暂时存父 id
            Resource  Presource = resourceService.selectById(res.getPid());
            res.setPcodes(Presource.getPcodes()+"["+Presource.getCode()+"],");
            res.setLevels(Presource.getLevels()+1);
            res.setIsopen(1);
            res.setUpdatetime(new Date());
        }
    }


    @ApiOperation("修改资源")
    @RequiresAuthentication
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @RequiresPermissions("resource:edit")
    @PostMapping("/edit")
    public ResultData editResource(@RequestBody ResourceVo resvo) {
        if(ToolUtil.isNotEmpty(resvo)) {
            resSetPcode(resvo);
            Resource res = new Resource();
            BeanUtils.copyProperties(resvo,res);
            res.setUpdatetime(new Date());
            resourceService.updateById(res);
            return new ResultData().succ(200,"更新成功");
        }
        return new ResultData().fail(400,"更新失败");
    }


    @ApiOperation("删除资源")
    @RequiresAuthentication
    @RequiresPermissions("resource:del")
    @ApiImplicitParams({ @ApiImplicitParam(paramType = "header", dataType = "String", name = "token", value = "token", required = true) })
    @PostMapping("/del")
    public ResultData delResource(@ApiJsonObject(name = "Resource_delResource_Map", value = {
            @ApiJsonProperty(key = "resid",  example = "1",  description = "资源ID", type = int.class, required = true) })
            @RequestBody Map<String,Object> params) {
        Integer resid = (Integer)params.get("resid");
        boolean  flag =  resourceService.deleteById(resid);
        if(flag == true) {
            return new ResultData().succ(200,"删除成功");
        }
        return new ResultData().fail(400,"删除失败");
    }



}

